<?php
/**
 * 
 */

/*
 * 
 */

/**
 * Tag Class
 *
 * @author anthony
 */
class Tag implements Stashable {
    protected $tag_identifier = false;
    protected $tag_id = false;
    protected $tag_name = false;
    protected $isValid = false;
    protected $numberProjects = false;
    protected $numberProjectsVisible = false;
    
    /**
     * Constructor for Tag, resolves the identifer and instanciates if possible
     * @global integer/string $sql_conx
     * @param integer/string $identifier
     * @return boolean 
     */
    public function __construct($identifier) {
	global $sql_conx;
	
	$this->tag_identifier = $identifier;
	try {
	    $row =& $this->resolveIdentifier();
	    if($row !== false) {
		$this->tag_id = $row['tid'];
		$this->tag_name = $row['tag'];
		$this->isValid = true;
	    } else {
		return false;
	    }
	} catch(InvalidArgumentException $e) {
	    // Someone passed null
	    $this->tag_id = -1;
	    $this->isValid = false;
	    return false;
	}
	
	
    }
    
    /**
     * Destructor for this class 
     */
    public function __destruct() {}
    
    /**
     * We are supplying 2 methods of creating a class of this type.
     * We can do it by ID number
     * or we can do it by the alias (eg www....../alias)
     * This class will work out who is what
     * @global integer/string $sql_conx
     * @return boolean
     * @throws InvalidArgumentException 
     */
    protected function &resolveIdentifier() {
	global $sql_conx;
	// Do we trust the data coming in?
	$identifier_e = $sql_conx->real_escape_string($this->tag_identifier);
	$columns = '`tag`, `tid`';
	if(is_null($this->tag_identifier)) {	    
	    throw new InvalidArgumentException("Cannot resolve null.");
	} elseif(is_int($this->tag_identifier)) {
	    $query = $sql_conx->query("SELECT {$columns} FROM `user_tags` WHERE `tid`='{$identifier_e}' LIMIT 1;");
	    if($query !== false && mysqli_num_rows($query) === 1) {
		$row = $query->fetch_assoc();
		return $row;
	    }
	    trigger_error('Could not resolve via id number `'.$this->tag_identifier.'`');
	    unset($query);
	    $res_by_num = false;
	}
	// Try to resolve the alias
	$query = $sql_conx->query("SELECT {$columns} FROM `user_tags` WHERE `tag`='{$identifier_e}' LIMIT 1;");
	if($query !== false && mysqli_num_rows($query) === 1) {
	    $row = $query->fetch_assoc();
	    return $row;
	}
	trigger_error('Could not resolve via tag name `'.$this->tag_identifier.'`');
	unset($query);
	$res_by_num = false;
	unset($query);
	$false = false; // returning by reference, slave variable
	return $false;
    }

    /**
     * Is the tag valid?
     * @return boolean
     */
    public function isValid() {
	return $this->isValid;
    }
    
    /**
     * Return the tags ID
     * @return string 
     */
    public function getID() {
	return $this->tag_id;
    }
    
    /**
     * Return the tags name
     * @return string 
     */
    public function getName() {
	return $this->tag_name;
    }
    
    /**
     * Return the tags URL /tag/*
     * @return string 
     */
    public function getURL() {
	return '/tag/'.$this->getName();
    }
    
    /**
     * Return the number of projects using this tag
     * @return int 
     */
    public function getNumberOfProjects() {
	global $sql_conx;
	if($this->numberProjects === false) {
	    $query = $sql_conx->query("SELECT COUNT(`tid`) FROM `tool_tags` WHERE `tid`='{$this->tag_id}'");
	    if($query !== false) {
		$row = $query->fetch_row();
		$this->numberProjects = $row[0];
	    } else {
		$this->numberProjects = 0;
	    }
	}
	return $this->numberProjects;
    }
    
    /**
     * Return the number of projects using this tag that are visible
     * @return int 
     */
    public function getNumberOfProjectsVisible() {
	global $sql_conx;
	if($this->numberProjectsVisible === false) {
	    $query = $sql_conx->query("SELECT COUNT(`pid`) FROM `tool_project` WHERE `visible`='1' AND `pid` IN (SELECT `pid` FROM `tool_tags` WHERE `tid`='{$this->tag_id}');");
	    if($query !== false) {
		$row = $query->fetch_row();
		$this->numberProjectsVisible = $row[0];
	    } else {
		$this->numberProjectsVisible = 0;
	    }
	}
	return $this->numberProjectsVisible;
    }
}

?>
